package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Sign;
import cn.wolfcode.crm.mapper.SignMapper;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ISignService;
import cn.wolfcode.crm.util.CRMConstant;
import cn.wolfcode.crm.util.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class SignServiceImpl implements ISignService {
    @Autowired
    private SignMapper signMapper;

    public void save(Sign record) {
        signMapper.insert(record);
    }


    public void update(Sign record) {
        signMapper.updateByPrimaryKey(record);
    }

    public Sign get(Long id) {

        return signMapper.selectByPrimaryKey(id);
    }

    public List<Sign> getAll() {

        return signMapper.selectAll();
    }

    public PageResult<Sign> query(ObjectQuery qo) {
        int totalCount = signMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<Sign>(totalCount, Collections.<Sign>emptyList());
        }
        return new PageResult<Sign>(totalCount, signMapper.queryForList(qo));
    }


    //改变保险状态并且 需要当前的用户 和当前时间new date
    public void changStateIn() {
        //获取到当前用户
        Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();
        //System.out.println(principal+"这是当前用户");
        //创建当前对象从当前对象中去封装
        Sign sign = new Sign();
        sign.setEmp(principal);
        //然后设置签到时间
        sign.setSingnin(new Date());
        //设置签到状态
        sign.setStatus(CRMConstant.sign.NOMAL);
        //改变状态值
        signMapper.changStateIn(sign.getId());
        //把这条数据保存进表中
        signMapper.insert(sign);
    }

    //改变保险状态并且 需要拿到当前的用户 和当前时间new date
    public void changStateOut() {
        Employee principal = (Employee) SecurityUtils.getSubject().getPrincipal();
        //System.out.println(principal+"这是当前用户");
        //创建当前对象从当前对象中去封装
        Sign sign = new Sign();
        sign.setEmp(principal);
        //然后设置签退时间
        sign.setSingout(new Date());
        //改变状态值
        sign.setStatus(CRMConstant.sign.AUDITED);
        //然后改变这条数据
        signMapper.changStateOut(sign);

    }

    public int getStateByEmployee() {
        //获取当前用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //获取用户的id
        if (employee != null) {
            SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
            Date beginDate = new Date();
            Calendar date = Calendar.getInstance();
            date.setTime(beginDate);
            date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
            try {
                Date endDate = dft.parse(dft.format(date.getTime()));
                Sign sign = signMapper.getSignByInDate(employee.getId(), DateUtil.getEndDate(endDate));
                System.out.println(DateUtil.getEndDate(endDate));
                if (sign == null) {
                    return -1;
                }
                if (sign.getSingout() == null) {
                    return 1;
                }
                return 0;
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        return -2;
    }
}
